﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using PowerCreator.LMS.Logic.Fpager;
using System.Text;

//using PowerCreator.LMS.Logic.Common;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.ViewQuery;



public partial class sysadmin_PaperManager_PaperUserList : PowerCreator.LMS.Web.Base.BasePage
{

    ManagerPaper objManagerPaper = new ManagerPaper();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();
    const string SEARCH_USERPAPERHISTORY_PAPER_TUSER = "select UserPaperHistory.UPHistoryID,UserPaperHistory.FKUPHistoryUserID, UserPaperHistory.FKUPHistoryProjectID,(select TrainproName from TTrainProject where TrainproID=UserPaperHistory.FKUPHistoryProjectID) as ProjectName,(select TrainclaName from TTrainClass where TTrainClass.TrainclaID=UserPaperHistory.FKUPHistoryClassID) as ClassName,UserPaperHistory.UPHistoryReadState,UserPaperHistory.UPHistoryReadUserID,UserPaperHistory.FKUPHistoryClassID,UserPaperHistory.FKUPHistoryCourseID,UserPaperHistory.FKUPHistoryPaperID,UserPaperHistory.UPHistoryStartTime,UserPaperHistory.UPHistorySubmitTime,UserPaperHistory.UPHistoryScore,Paper.PaperTestTime,TUser.UserName,UserPaperHistory.UPHistoryReadTime from UserPaperHistory,Paper,TUser where Paper.PaperID=UserPaperHistory.FKUPHistoryPaperID and TUser.UserID=UserPaperHistory.FKUPHistoryUserID {0}";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["PaperID"] = Request.QueryString["p"].ToString();
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lab_PaperList_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Username.Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Readstate.Text = GetLanageValueByKey("lab_PaperUserList_ReadState", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_seach.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Back.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        dpdlst_readstate.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_readstate.Items[1].Text = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
        dpdlst_readstate.Items[2].Text = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }
    private string GetSearch()
    {
        int paperID=int.Parse(ViewState["PaperID"].ToString());
        int readstate=int.Parse(dpdlst_readstate.SelectedValue);
        string username = txt_username.Text.Trim();
        StringBuilder search = new StringBuilder();
        search.Append(" and UserPaperHistory.FKUPHistoryPaperID='" + paperID + "'");
        if (!string.IsNullOrEmpty(username))
        {
            search.Append(" and TUser.UserName like '%" + username + "%' ");
        }
        if (readstate != 2)
        {
            search.Append(" and UPHistoryReadState=" + readstate + "");
        }
        string strSearch = string.Format(SEARCH_USERPAPERHISTORY_PAPER_TUSER, search.ToString());
        return strSearch;
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objViewQuery.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        dt.Columns.Add("url");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["url"] = "PaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString() + "&ProjectID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString()+ "&ClassID=" + dt.Rows[i]["FKUPHistoryClassID"].ToString() + "&UserID=" + dt.Rows[i]["FKUPHistoryUserID"].ToString() + "&CourseID=" +dt.Rows[i]["FKUPHistoryCourseID"].ToString();
        }
        gvUserList.DataSource = dt;
        gvUserList.DataBind();

        PreAspNetPager(AspNetPager1);
    }


    protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btn_view")
        {
            Script.RedirectURL("ReadedPaper.aspx?" + e.CommandArgument.ToString());
        }
    }
    protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {

            e.Row.Cells[1].Text = GetLanageValueByKey("lab_ProjectList_ProjectName", PowerCreator.LMS.Language.LanagetItemType.Page);

            e.Row.Cells[2].Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[3].Text = GetLanageValueByKey("lbl_StudentManager_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[4].Text = GetLanageValueByKey("lbl_AddClass_TrainclaStartTime", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[5].Text = GetLanageValueByKey("lbl_AddClass_TrainclaEndTime", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[6].Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[7].Text = GetLanageValueByKey("msg_PaperUserList_pingjuanshijian", PowerCreator.LMS.Language.LanagetItemType.Page);

            e.Row.Cells[8].Text = GetLanageValueByKey("msg_PaperUserList_pingjuanren", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");
            LinkButton btn_view = (LinkButton)e.Row.Cells[9].FindControl("btn_view");
            btn_view.Text = GetLanageValueByKey("btn_BuilderPaper_Yulan", PowerCreator.LMS.Language.LanagetItemType.Page);
            if(e.Row.Cells[6].Text.Trim()=="0")
            {
                e.Row.Cells[6].Text = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                e.Row.Cells[6].Text = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        } 

    }

    protected void btn_seach_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }

}
